package com.mingtu.guards.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.luck.picture.lib.config.PictureMimeType;
import com.mingtu.guards.R;
import com.mingtu.guards.bean.FileListBean;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FileDocAdapter extends RecyclerView.Adapter<FileDocAdapter.ActivityHolder> implements View.OnClickListener, View.OnLongClickListener {
    private Context context;
    private List<FileListBean.PageBean.ListBean> mData;
    private OnItemClickListener mOnItemClickListener = null;
    private OnItemLongClickListener mOnItemLongClickListener = null;
    private HashMap<Integer, Boolean> status = new HashMap<>();
    private Activity activity;

    public FileDocAdapter(Context context, List<FileListBean.PageBean.ListBean> mData) {
        this.mData = mData;
        this.context = context;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_file_data, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        viewHolder.itemView.setOnClickListener(this);
        viewHolder.itemView.setOnLongClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {
        if (mData != null) {
            if (mData.size() > 0) {

                FileListBean.PageBean.ListBean listBean = mData.get(position);
                String name = listBean.getName();
                String createTime = listBean.getCreateTime();
                String docUrl = listBean.getUrl();

                boolean isHasVideo = PictureMimeType.isUrlHasVideo(docUrl);
                boolean isHasImage = PictureMimeType.isUrlHasImage(docUrl);

                if(isHasVideo){
                    holder.ivImg.setBackgroundResource(R.mipmap.img_file_type4);
                }else if(isHasImage){
                    holder.ivImg.setBackgroundResource(R.mipmap.img_file_type3);
                }else{
                    holder.ivImg.setBackgroundResource(R.mipmap.img_file_type2);
                }


                holder.tvFileName.setText(name);
                holder.tvTime.setVisibility(View.VISIBLE);
                holder.tvTime.setText(createTime);

                boolean isCheck = status.get(position);
                if (isCheck) {
                    holder.ivCheck.setVisibility(View.VISIBLE);
                } else {
                    holder.ivCheck.setVisibility(View.GONE);
                }
            }

        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mOnItemLongClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemLongClickListener.onItemLongClick(v, (int) v.getTag());
        }
        return false;
    }


    public void upData(List<FileListBean.PageBean.ListBean> data) {
        mData = data;
        for (int i = 0; i < mData.size(); i++) {
            status.put(i, false);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mData != null) {
            mData.clear();
        }
    }

    /**
     * 修改状态
     *
     * @param position
     */
    public void updateStatus(int position) {
        for (int i = 0; i < mData.size(); i++) {
            status.put(i, false);
        }
        status.put(position, true);
        notifyDataSetChanged();
    }

    /**
     * 重置状态
     */
    public void resetStatus() {
        for (int i = 0; i < mData.size(); i++) {
            status.put(i, false);
        }
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.mOnItemLongClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_img)
        ImageView ivImg;
        @BindView(R.id.iv_check)
        ImageView ivCheck;
        @BindView(R.id.tv_file_name)
        TextView tvFileName;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.layout)
        LinearLayout layout;

        public ActivityHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
